//创建页面
import 'package:flutter/material.dart';
import 'package:smart/navigator/bottom_navigator.dart';
import 'package:smart/page/login_page.dart';
import 'package:smart/page/registration_page.dart';
import 'package:smart/page/video_detail_page.dart';

typedef RouteChangeListener(RouteStatusInfo current, RouteStatusInfo pre);

pageWrap(Widget child) {
  return MaterialPage(key: ValueKey(child.hashCode), child: child);
}

//获取routeStatus 在页面中的位置
int getPageIndex(List<MaterialPage> pages, RouteStatus routeStatus) {
  for (int i = 0; i < pages.length; i++) {
    MaterialPage page = pages[i];
    if (getStatus(page) == routeStatus) {
      return i;
    }
  }
  return -1;
}

//自定义路由封装，路由状态
enum RouteStatus { login, registration, home, detail, unknown }

//获取page对应的RouteStatus
RouteStatus getStatus(MaterialPage page) {
  print('getStatus  page:${page.child.key}');
  if (page.child is LoginPage) {
    return RouteStatus.login;
  } else if (page.child is RegistrationPage) {
    return RouteStatus.registration;
  } else if (page.child is VideoDetailPage) {
    return RouteStatus.detail;
    //} else if (page.child is HomePage) {
  } else if (page.child is BottomNavigator) {
    return RouteStatus.home;
  } else {
    return RouteStatus.unknown;
  }
}

//路由信息
class RouteStatusInfo {
  final RouteStatus routeStatus;
  final Widget page;
  RouteStatusInfo(this.routeStatus, this.page);
}

// 监听路由主页跳转
// 感知当前页面是否压后台
class HiNavigator extends _RouteJumpListener {
  static HiNavigator? _instance;
  RouteJumpListener? _routeJump;
  List<RouteChangeListener> _listenser = [];
  RouteStatusInfo? _current; //定义一个打开过的页面信息
  //首页底部的tab
  RouteStatusInfo? _bottomTab;

  HiNavigator._();

  static HiNavigator getInstance() {
    if (_instance == null) {
      _instance = HiNavigator._();
    }
    return _instance!;
  }

  // 首页底部tab 切换监听
  void onBottomTabChange(int index, Widget page) {
    _bottomTab =
        RouteStatusInfo(RouteStatus.home, page); // 底部导航切换后，构建一个RouteStatusInfo
    _notify(_bottomTab!);
  }

  //注册路由跳转逻辑
  void registerRouteJump(RouteJumpListener routeJumpListener) {
    this._routeJump = routeJumpListener;
  }

  //监听路由页面的跳转
  void addListener(RouteChangeListener listener) {
    if (!_listenser.contains(listener)) {
      _listenser.add(listener);
    }
  }

  //移除监听
  void removeListener(RouteChangeListener listener) {
    _listenser.remove(listener);
  }

  //通知路由页面的变化
  void notify(List<MaterialPage> currentPages, List<MaterialPage> prePages) {
    if (currentPages == prePages) return;
    var current =
        RouteStatusInfo(getStatus(currentPages.last), currentPages.last.child);

    _notify(current);
  }

  @override
  void onJumpTo(RouteStatus routeStatus, {Map? args}) {
    _routeJump?.onJumpTo(routeStatus, args: args);
  }

  void _notify(RouteStatusInfo current) {
    if (current!.page is BottomNavigator && _bottomTab != null) {
      //如果打开的是首页，则明确到首页具体的tab
      current = _bottomTab!;
    }

    print('hi_navigator:currrent:${current.page}');
    print('hi_navigator:pre:${_current?.page}');
    _listenser.forEach((listener) {
      listener(current, _current!);
    });
    _current = current;
  }
}

//抽象类提供HiNavigator 实现
abstract class _RouteJumpListener {
  void onJumpTo(RouteStatus routeStatus, {Map args});
}

typedef OnJumpTo = void Function(RouteStatus routeStatus, {Map? args});

//定义一个路由跳转逻辑要实现的功能
class RouteJumpListener {
  final OnJumpTo onJumpTo;

  RouteJumpListener({required this.onJumpTo});
}
